package com.jy.novel.dao.system;

import com.jy.novel.common.JdbcHelper;
import com.jy.novel.pojo.system.RoleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;

/**
 * Created by yangqy on 4/29/16.
 */

@Repository
public class RoleDao {


    @Autowired
    private JdbcHelper dbHelper;

    public RoleDao() {
    }

    public List<RoleInfo> findRoleList(int adminRoleId, int defaultRoleId) {
        String sql = "(select * from system_role where id in(?,?) order by id asc) union all  (select * from system_role where id not in(?,?) order by name asc) ";
        return this.dbHelper.queryForListBean(sql, new Integer[]{Integer.valueOf(adminRoleId), Integer.valueOf(defaultRoleId), Integer.valueOf(adminRoleId), Integer.valueOf(defaultRoleId)},RoleInfo.class);
    }

    public boolean isExistRole(Long roleId) {
        String sql = "select count(*) from system_role where id = ? ";
        return this.dbHelper.queryForNumber(sql, new Object[]{roleId}).longValue() > 0L;
    }

    public RoleInfo getRoleInfo(Long id) {
        String sql = "select * from system_role where id = ? ";
        return (RoleInfo)this.dbHelper.queryForBean(sql, new Object[]{id}, RoleInfo.class);
    }

    public boolean isExistRole(String name, String des) {
        String sql = "select count(*) from system_role where name = ? and des = ? ";
        return this.dbHelper.queryForNumber(sql, new Object[]{name, des}).longValue() > 0L;
    }

    public int updateRoleInfo(Long id, String name, String des) {
        String sql = "update system_role set name = ?,des = ? where id = ? ";
        return this.dbHelper.update(sql, new Object[]{name, des, id});
    }

    public int deleteRole(Long id) {
        String sql = "delete from system_role where id = ?";
        return this.dbHelper.update(sql, new Object[]{id});
    }

    public int addRole(RoleInfo role) {
        HashMap cl = new HashMap();
        cl.put("name", role.getName());
        cl.put("remark", role.getRemark());
        return this.dbHelper.simpleInsert("system_role", cl);
    }

    public Number addRoleReturnId(RoleInfo role) {
        HashMap cl = new HashMap();
        cl.put("name", role.getName());
        cl.put("remark", role.getRemark());
        return this.dbHelper.simpleInsertReturnAutoField("system_role", cl);
    }
}

